package edu.ynu.e_platform.service;

import edu.ynu.e_platform.common.service.LogicService;
import edu.ynu.e_platform.common.utils.TokenUtils;
import edu.ynu.e_platform.dao.UserDao;
import edu.ynu.e_platform.dto.ItemDTO;
import edu.ynu.e_platform.dto.UserDTO;
import edu.ynu.e_platform.entities.Business;
import edu.ynu.e_platform.entities.User;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author manwen Yang
 */
@Slf4j
@Service
public class UserService extends LogicService<UserDao, User,Long> {

    @Resource
    ModelMapper modelMapper;

    public UserService(@Autowired UserDao lr) {
        super(lr);
    }

    public UserDTO login(String userName, String password){

        try {
            User user = getDAO().findByUsernameAndPassword(userName, password);

            if(user!=null){
                UserDTO result = modelMapper.map(user, UserDTO.class);

                //设置token
                String token = TokenUtils.genToken(result.getId(),result.getPassword());
                result.setToken(token);

                return result;
            }else {
                return null;
            }

        } catch (Exception e) {
            log.warn(e.toString());
        }

        return null;
    }

    public User findUser(Long userId){
        return getDAO().findUser(userId);
    }
}
